package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.CategoryCreateException;
import com.sky.mapper.ClassifyMapper;
import com.sky.result.PageResult;
import com.sky.service.ClassifyService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ClassifyServiceImpl implements ClassifyService {
    private final ClassifyMapper classifyMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        List<Category> categoryList = classifyMapper.getAllCateGory(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        PageInfo<Category> categoryPageInfo = PageInfo.of(categoryList);
        return new PageResult(categoryPageInfo.getTotal(), categoryPageInfo.getList());
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        String name = categoryDTO.getName();
        Integer count = classifyMapper.selectByName(name);
        if (count > 0) {
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_NAME);
        }
        Integer sort = categoryDTO.getSort();
        Integer sortCount = classifyMapper.selectBySort(sort);
        if (sortCount > 0) {
            throw new CategoryCreateException(MessageConstant.REPEAT_SORT_NAME);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        classifyMapper.addCategory(category);
    }

    @Override
    public void deleteCategory(String id) {
        int count = classifyMapper.getDish(id);
        if (count > 0){
            throw new CategoryCreateException(MessageConstant.DUPLICATE_DISH_NAMES);
        }
        int setmealCount = classifyMapper.getSetmeal(id);
        if (setmealCount > 0){
            throw new CategoryCreateException(MessageConstant.DUPLICATE_DISH_NAMES);
        }
        classifyMapper.deleteCategory(id);
    }

    @Override
    public void statusCategory(String id, int status) {
        Category category = classifyMapper.selectById(id);
        category.setStatus(status);
        classifyMapper.update(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        String name = categoryDTO.getName();
        Long id = categoryDTO.getId();
        Integer sort = categoryDTO.getSort();
        Integer count = classifyMapper.selectByNameNotSelf(name,id);
        if (count > 0) {
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_NAME);
        }
        Integer sortCount = classifyMapper.selectBySort(sort);
        if (sortCount > 0) {
            throw new CategoryCreateException(MessageConstant.REPEAT_SORT_NAME);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        classifyMapper.update(category);
    }

    @Override
    public List<Category>  getCategoryList(CategoryDTO categoryDTO) {
        List<Category> categoryList = classifyMapper.getCateGory(categoryDTO.getType());
        return categoryList;
    }
}
